﻿namespace ufida.u9.ait.openapi
{
	using System;
	using System.Collections.Generic;
	using System.Text;
	using ufida.u9.ait.openapi.utils;
    using UFIDA.U9.Base.PropertyTypes;
    using UFIDA.U9.MO.MO;
    using UFIDA.U9.PM.Rcv;
	using UFSoft.UBF.AopFrame;
	using UFSoft.UBF.Business;
	using UFSoft.UBF.Util.Context;

	/// <summary>
	/// RCVLineApproveSV partial 
	/// </summary>	
	public partial class RCVLineApproveSV 
	{	
		internal BaseStrategy Select()
		{
			return new RCVLineApproveSVImpementStrategy();	
		}		
	}
	
	#region  implement strategy	
	/// <summary>
	/// Impement Implement
	/// 
	/// </summary>	
	internal partial class RCVLineApproveSVImpementStrategy : BaseStrategy
	{
		public RCVLineApproveSVImpementStrategy() { }

		public override object Do(object obj)
		{						
			RCVLineApproveSV bpObj = (RCVLineApproveSV)obj;

			try
			{
                if (bpObj.Data == null || bpObj.Data.Count == 0)
                    throw new Exception("传入参数为空");
                List<CopyOfCommonArchiveDataDTOData> retlist = new List<CopyOfCommonArchiveDataDTOData>();
				foreach(var dto in bpObj.Data)
				{
                    Receivement doc = Receivement.Finder.Find($"DocNo='{dto.DocNo}'");
					if (doc == null)
					{
                        //新增逻辑
                        throw new Exception($"单号:{dto.DocNo}不存在!");
                    }
					else
					{
						//修改逻辑
						RcvUpdate(dto,doc);
						//审核逻辑
						LineApprove(dto, doc);

                        retlist.Add(new CopyOfCommonArchiveDataDTOData()
                        {
                            ID = dto.ID,
                            Code = dto.DocNo
                        });

                    }
                }
                return ResultUtils.success(bpObj, bpObj.Data, retlist);
            }
			catch (Exception ex)
			{

                return ResultUtils.error(bpObj, bpObj.Data, ex);
            }
		}

        private void LineApprove(CopyOfOBAReceivementDTOData dto, Receivement doc)
        {
            //构造实体行缓存
            Dictionary<int, RcvLine> linedic = new Dictionary<int, RcvLine>();
            foreach (var line in doc.RcvLines)
            {
                if (!linedic.ContainsKey(line.DocLineNo))
                {
                    linedic.Add(line.DocLineNo, line);
                }
            }
            //行审核
            using(ISession session = Session.Open())
            {
                foreach (var dtoline in dto.RcvLines)
                {
                    RcvLine line = linedic[dtoline.DocLineNo];
                    if (line.Status != RcvStatusEnum.Approving)
                        throw new Exception($"单号[{dto.DocNo}]行号[{dtoline.DocLineNo}]状态不为审核中!");

                    line.Status = RcvStatusEnum.InStoreConfirmed;
                    line.ActivateType = UFIDA.U9.PM.Enums.ActivateTypeEnum.UIUpdate;
                }

                session.Commit();
            }
            
            
        }

        private void RcvUpdate(CopyOfOBAReceivementDTOData dto, Receivement doc)
        {
            //构造实体行缓存
            Dictionary<int, RcvLine> linedic = new Dictionary<int, RcvLine>();
            foreach (var line in doc.RcvLines)
            {
                if (!linedic.ContainsKey(line.DocLineNo))
                {
                    linedic.Add(line.DocLineNo, line);
                }
            }
            //修改行
            using (ISession session = Session.Open())
			{
                foreach (var dtoline in dto.RcvLines)
                {
                    if (!linedic.ContainsKey(dtoline.DocLineNo))
                        throw new Exception($"单号[{dto.DocNo}]行号[{dtoline.DocLineNo}]不存在!");
                    RcvLine line = linedic[dtoline.DocLineNo];

                    //实到数量
                    if (dtoline.ArriveQtyTU > 0)
                    {
                        line.ArriveQtyTU = dtoline.ArriveQtyTU;
                        //line.ArriveQtyTBU = dtoline.ArriveQtyTU;
                        line.ArriveQtySU = dtoline.ArriveQtyTU;
                        line.ArriveQtyPU = dtoline.ArriveQtyTU;
                        line.ArriveQtyCU = dtoline.ArriveQtyTU;

                        dtoline.EyeballingQtyTU = dtoline.ArriveQtyTU;
                    }

                    //点收数量
                    if (dtoline.EyeballingQtyTU > 0)
                    {
                        line.EyeballingQtyTU = dtoline.EyeballingQtyTU;
                        //line.EyeballingQtyTBU = dtoline.EyeballingQtyTU;
                        line.EyeballingQtySU = dtoline.EyeballingQtyTU;
                        line.EyeballingQtyPU = dtoline.EyeballingQtyTU;
                        line.EyeballingQtyCU = dtoline.EyeballingQtyTU;

                        dtoline.RcvQtyTU = dtoline.EyeballingQtyTU;
                    }

                    //实收数量
                    if (dtoline.RcvQtyTU > 0)
                    {
                        line.RcvQtyTU = dtoline.RcvQtyTU;
                        //line.RcvQtyTBU = dtoline.RcvQtyTU;
                        line.RcvQtySU = dtoline.RcvQtyTU;
                        line.RcvQtyPU = dtoline.RcvQtyTU;
                        line.RcvQtyCU = dtoline.RcvQtyTU;

                        //可立账数量
                        line.CanAPConfirmQtyTU = dtoline.RcvQtyTU;
                        line.CanAPConfirmQtySU = dtoline.RcvQtyTU;
                        line.CanAPConfirmQtyPU = dtoline.RcvQtyTU;
                        line.CanAPConfirmQtyCU = dtoline.RcvQtyTU;
                    }

                    //计算金额
                    CalMoney(line);
                }

                session.Commit();
			}
            

			
			
        }

        private static void CalMoney(RcvLine line)
        {
            Round moneyRound = line.Receivement.TC.MoneyRound;
            decimal netmoney;//不含税金额
            decimal tax;//税额
            decimal totalmoney;//价税合计

            if (line.ArriveQtyPU != line.PlanQtyPU)
            {
                //依据实到数量计算实到金额
                if (line.IsPriceIncludeTax)
                {
                    //价格含税
                    totalmoney = moneyRound.GetRoundValue(line.ArriveQtyPU * line.FinallyPriceTC);
                    netmoney = moneyRound.GetRoundValue(totalmoney / (1 + line.TaxRate));
                    tax = totalmoney - netmoney;
                }
                else
                {
                    //价格不含税
                    netmoney = moneyRound.GetRoundValue(line.ArriveQtyPU * line.FinallyPriceTC);
                    tax = moneyRound.GetRoundValue(netmoney * line.TaxRate);
                    totalmoney = netmoney + tax;
                }
                //实收价税合计
                line.ArriveTotalMnyTC = totalmoney;
                line.ArriveTotalMnyFC = totalmoney;
                line.ArriveTotalMnyAC = totalmoney;
                //实收未税金额
                line.ArriveTotalNetMnyTC = netmoney;
                line.ArriveTotalNetMnyFC = netmoney;
                line.ArriveTotalNetMnyAC = netmoney;
                //实收税额
                line.ArriveTotalTaxTC = tax;
                line.ArriveTotalTaxFC = tax;
                line.ArriveTotalTaxAC = tax;
            }
            

            if(line.CanAPConfirmQtyPU != line.PlanQtyPU)
            {
                //依据可立账数量计算实收金额
                if (line.IsPriceIncludeTax)
                {
                    //价格含税
                    totalmoney = moneyRound.GetRoundValue(line.CanAPConfirmQtyPU * line.FinallyPriceTC);
                    netmoney = moneyRound.GetRoundValue(totalmoney / (1 + line.TaxRate));
                    tax = totalmoney - netmoney;
                }
                else
                {
                    //价格不含税
                    netmoney = moneyRound.GetRoundValue(line.CanAPConfirmQtyPU * line.FinallyPriceTC);
                    tax = moneyRound.GetRoundValue(netmoney * line.TaxRate);
                    totalmoney = netmoney + tax;
                }

                //实收价税合计
                line.TotalMnyTC = totalmoney;
                line.TotalMnyFC = totalmoney;
                line.TotalMnyAC = totalmoney;
                //实收未税金额
                line.TotalNetMnyTC = netmoney;
                line.TotalNetMnyFC = netmoney;
                line.TotalNetMnyAC = netmoney;
                //实收税额
                line.TotalTaxTC = tax;
                line.TotalTaxFC = tax;
                line.TotalTaxAC = tax;
            }
            
        }
    }

	#endregion
	
	
}